package com.sugar.real.estate.module.business.dal.mysql.userbuildingconsult;

import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.mybatis.core.mapper.BaseMapperX;
import com.sugar.real.estate.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.sugar.real.estate.module.business.controller.admin.userbuildingconsult.vo.UserBuildingConsultPageReqVO;
import com.sugar.real.estate.module.business.controller.admin.userbuildingconsult.vo.UserBuildingConsultRespVO;
import com.sugar.real.estate.module.business.dal.dataobject.userbuildingconsult.UserBuildingConsultDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户楼盘咨询 Mapper
 *
 * @author Ysugar
 */
@Mapper
public interface UserBuildingConsultMapper extends BaseMapperX<UserBuildingConsultDO> {

    default PageResult<UserBuildingConsultDO> selectPage(UserBuildingConsultPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<UserBuildingConsultDO>()
                .eqIfPresent(UserBuildingConsultDO::getUserId, reqVO.getUserId())
                .eqIfPresent(UserBuildingConsultDO::getFollowId, reqVO.getFollowId())
                .eqIfPresent(UserBuildingConsultDO::getType, reqVO.getType())
                .likeIfPresent(UserBuildingConsultDO::getName, reqVO.getName())
                .eqIfPresent(UserBuildingConsultDO::getMobile, reqVO.getMobile())
                .betweenIfPresent(UserBuildingConsultDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(UserBuildingConsultDO::getId));
    }


    @Select("<script>" +
            "select" +
            " count(bubc.id)" +
            "from business_user_building_consult bubc " +
            "where bubc.deleted = 0 " +
            "<if test=\"param1 != null\"> and bubc.user_id = #{param1} </if>" +
            "<if test=\"param2 != null\"> and bubc.follow_id = #{param2} </if>" +
            "<if test=\"param3 != null\"> and bubc.`type` = #{param3} </if>" +
            "<if test=\"param4 != null and param5 != null\"> and bubc.create_time between #{param4} and #{param5} </if>" +
            "<if test=\"param6 != null and param6 != ''\"> and bubc.name like concat('%', #{param6}, '%') </if>" +
            "<if test=\"param7 != null and param7 != ''\"> and bubc.mobile like concat('%', #{param7}, '%') </if>" +
            "</script>")
    Long selectPageCount(Long userId, Long followId, Integer type, LocalDateTime startTime, LocalDateTime endTime, String name, String mobile);


    @Select("<script>" +
            "select" +
            " bubc.*," +
            " if(bubc.`type` = 0, ba.name_zh_cn, if(bubc.`type` = 1, brh.name_zh_cn, if(bubc.`type` = 2, bbh.location_zh_cn, bdp.title_zh_cn))) as name_zh_cn," +
            " if(bubc.`type` = 0, ba.name_en_us, if(bubc.`type` = 1, brh.name_en_us, if(bubc.`type` = 2, bbh.location_en_us, bdp.title_en_us))) as name_en_us " +
            "from business_user_building_consult bubc " +
            "left join business_apartment ba on bubc.`type` = 0 and bubc.follow_id = ba.id and ba.deleted = 0 " +
            "left join business_ready_house brh on bubc.`type` = 1 and bubc.follow_id = brh.id and brh.deleted = 0 " +
            "left join business_build_house bbh on bubc.`type` = 2 and bubc.follow_id = bbh.id and bbh.deleted = 0 " +
            "left join business_design_plan bdp on bubc.`type` = 3 and bubc.follow_id = bdp.id and bdp.deleted = 0 " +
            "where bubc.deleted = 0" +
            "<if test=\"param1 != null\"> and bubc.user_id = #{param1} </if>" +
            "<if test=\"param2 != null\"> and bubc.follow_id = #{param2} </if>" +
            "<if test=\"param3 != null\"> and bubc.`type` = #{param3} </if>" +
            "<if test=\"param4 != null and param5 != null\"> and bubc.create_time between #{param4} and #{param5} </if>" +
            "<if test=\"param6 != null and param6 != ''\"> and bubc.name like concat('%', #{param6}, '%') </if>" +
            "<if test=\"param7 != null and param7 != ''\"> and bubc.mobile like concat('%', #{param7}, '%') </if>" +
            "order by bubc.id desc " +
            "limit #{param8}, #{param9}" +
            "</script>")
    List<UserBuildingConsultRespVO> selectCustomPage(Long userId, Long followId, Integer type, LocalDateTime startTime, LocalDateTime endTime, String name, String mobile, int skipCount, Integer pageSize);
}